//index.js
//获取应用实例
const app = getApp()

Page({
  data: {
    hasUserInfo: false,
    status:true,
    canIUse: wx.canIUse('button.open-type.getUserInfo'),
    openid: "",
    nick_name: '',
    icon: "",
    avatarUrl: "",
    userInfo: "",
    person_info: "",
    // dialog
    showOneButtonDialog: false,
    oneButton: [{text: '确定'}],
    register:false,
    selected:2,
    loading:true
  },
  //事件处理函数
  clickme: function () {
    this.setData({ msg: "设置了data" })
    wx.showNavigationBarLoading()
  },

  // 获取openid
  getOpenid: function () {
    let that = this;
    wx.login(
      {
        success: function (res) {
          wx.request({
            method: "POST",
            url: "http://api_dev.wanxikeji.cn/api/codeExchangeOpenid",
            data: {
              code: res.code
            },
            success: function (response) {
              console.log(response);
              
              var openid = response.data.data.openid;
              // openid存缓存
              wx.setStorageSync('openid', openid);
              //  获取头像
             that.getname();
              // token存缓存
            
              // 个人信息存缓存
              if(response.data.data.info){

                var token = response.data.data.info.token;
                // console.log("token",token);
                
                wx.setStorageSync('token', token);
                //wx.setStorageSync('person_info', response.data.data.info);
                that.setData({
                  // icon: response.data.data.info.icon,
                  // nick_name: response.data.data.info.nick_name,
                  hasUserInfo: true,
                  register:false
                })
                // console.log("openid1",openid);  
              }
              else{
                that.setData({
                  register:true
                })
              }
              that.setData({
                openid: openid,
              })
              // 注册
              // that.getUserInfo();
            },

          })
        }
      })
  },
  reflash:function(){
    let that = this;
    // 获取缓存openid
    wx.getStorage({
      key: 'openid',
      success (res) {
        that.setData({
          openid:res.data
        })
        // console.log(res.data)
         // 更新token
        wx.request({
          method: "POST",
          url: "http://api_dev.wanxikeji.cn/api/refreshToken",
          data: {
            openid:that.data.openid
          },
          success: function (response) {
            // var openid = response.data.data.openid;
            // console.log("请求获取openid:" + openid);
            // // openid存缓存
            // wx.setStorageSync('openid', openid);
            // token存缓存
            var token = response.data.data.token;
            wx.setStorageSync('token', token);
          }
        })
      }
    })
  },
  getname:function () {
    let that = this;
   
    if (app.globalData.userInfo) {
      that.setData({
        icon: app.globalData.userInfo.avatarUrl,
        nick_name: app.globalData.userInfo.nickName,
        hasUserInfo: true
      })
    } else if (that.data.canIUse) {
      // 由于 getUserInfo 是网络请求，可能会在 Page.onLoad 之后才返回
      // 所以此处加入 callback 以防止这种情况
      app.userInfoReadyCallback = res => {
        that.setData({
          icon: app.globalData.userInfo.avatarUrl,
          nick_name: app.globalData.userInfo.nickName,
          hasUserInfo: true
        })
      }
    } else {
      // 在没有 open-type=getUserInfo 版本的兼容处理
      wx.getUserInfo({
        success: res => {
          
          app.globalData.userInfo = res.userInfo
          that.setData({
            icon: app.globalData.userInfo.avatarUrl,
            nick_name: app.globalData.userInfo.nickName,
            hasUserInfo: true
          })
        }
      })
    }
    wx.getStorage({
      key: 'person_info',
      success (res) {
        that.setData({
          nick_name:res.data.nick_name,
          icon:res.data.icon
        })
        if (that.data.nick_name == '') { 
          if (app.globalData.userInfo) {
            that.setData({
              icon: app.globalData.userInfo.avatarUrl,
              nick_name: app.globalData.userInfo.nick_name,
              hasUserInfo: true
            })
          } else if (that.data.canIUse) {
            // 由于 getUserInfo 是网络请求，可能会在 Page.onLoad 之后才返回
            // 所以此处加入 callback 以防止这种情况
            app.userInfoReadyCallback = res => {
              that.setData({
                icon: app.globalData.userInfo.avatarUrl,
                nick_name: app.globalData.userInfo.nickName,
                hasUserInfo: true
              })
            }
          } else {
            // 在没有 open-type=getUserInfo 版本的兼容处理
            wx.getUserInfo({
              success: res => {
                app.globalData.userInfo = res.userInfo
                that.setData({
                  icon: app.globalData.userInfo.avatarUrl,
                  nick_name: app.globalData.userInfo.nickName,
                  hasUserInfo: true
                })
              }
            })
          }
        } else {
          that.setData({
            icon: that.data.icon,
            nick_name: that.data.nick_name,
            hasUserInfo: true
          })
          
        }
      }
    })
   
  },
  onLoad: function () {  
    // 获取openid
    this.getOpenid();
    //  更新token
    this.reflash();
    //  获取头像
   //this.getname();
  
    var token=wx.getStorageSync('token')
    var status=wx.getStorageSync('setting_status')
   
    if(status!=''){
      if(status==1){
        this.setData({
          status:true
        })
      }
      if(status==2){
        this.setData({
          status:false
        })
      }
    }
    
    if(token){
    this.setData({
      token:token,
      
    })
    var userinfo=wx.getStorageSync('person_info')
    if(userinfo){
      
      this.setData({
        icon:userinfo.icon,
        nick_name:userinfo.nick_name,
        hasUserInfo:true,
        canIUse:true
      })
    }
    //console.log(this.data.hasUserInfo)
  }
    
  },
  //监听页面渲染
  onShow(){
     var token=wx.getStorageSync('token')
    var userinfo=wx.getStorageSync('person_info')
    
    if(token){
    this.setData({
      token:token,
    })
    if(userinfo){
      
      this.setData({
        icon:userinfo.icon,
        nick_name:userinfo.nick_name,
        hasUserInfo:true,
        canIUse:true,
       
      })
    }
    }
    this.setData({
      loading:false
    })
  },
  // 获取用户信息
  getUserInfo: function (e) {
    this.getOpenid()
    var that=this
    
    app.globalData.userInfo = e.detail.userInfo
    this.setData({
      userInfo: e.detail.userInfo,
      hasUserInfo: true,
      // 设置注册信息
      nick_name: e.detail.userInfo.nickName,
      icon: e.detail.userInfo.avatarUrl,
    })
   
    // console.log("openid2",this.data.openid);
    // 根据openid注册用户信息
    // console.log("openid",this.data.openid)
    if(that.data.register){
      wx.request({
        method: "POST",
        url: "http://api_dev.wanxikeji.cn/api/register",
        data: {
          openid: this.data.openid,
          nick_name: this.data.nick_name,
          icon: this.data.icon,
        },
        success: function (response) {
          if(response.data.msg == '用户已存在'){
        
            wx.request({
              url: 'http://api_dev.wanxikeji.cn/api/refreshToken',
              method: 'POST',
              data: {
                openid:response.data.data.openid
              },
              success(e){
                
                that.setData({
                  token: e.data.data.token
                })
              }
            })
          }else{
            wx.setStorage({
              data: response.data.data.token,
              key: 'token',
            })
            wx.setStorage({
              data: response.data.data,
              key: 'person_info',
            })
            
            that.setData({
              token: response.data.data.token
            })
          }
          // console.log("token",response);
          // // token存缓存
          // var token = response.data.data.token;
          // that.setData({
          //   token:token
          // })
          // wx.setStorageSync('token', token);
        }
      })
    }
    else{
      wx.request({
        url: 'http://api_dev.wanxikeji.cn/api/refreshToken',
        method:'POST',
        data:{
          openid:that.data.openid
        },
        success:function(res){
          var token=res.data.data.token
          wx.setStorageSync('token', token)
        }
      })
    }
  },
  getPhoneNumber (e) {
    console.log(e.detail.errMsg)
    console.log(e.detail.iv)
    console.log(e.detail.encryptedData)
  },
  loginOut(){
    var that=this
    wx.showModal({
      title: '提示',
      content: '确定退出？',
      success (res) {
        if (res.confirm) {
          that.setData({
            userInfo:'',
            person_info:'',
            hasUserInfo: false,
            canIUse:true
          })
          wx.setStorageSync('token', '')
          wx.setStorageSync('userInfo', '')
          wx.setStorageSync('person_info', '')
        } else if (res.cancel) {
          console.log('用户点击取消')
        }
      }
    })
  },
      // 提示登录
    islogin:function(){
      
      wx.showToast({
        title: '请登录',
        icon:'none'
      })
    },
    // 登录验证模块
    login_order:function () {
      let that = this;
      
      var token=wx.getStorageSync('token')

      if(token!=''){
        console.log(1)
        that.getname();
            wx.navigateTo({
              url: '../order_Form/order_Form',
              success: function(res) {
              },
            })
      }
      else{
         // 提示登录
         that.islogin();
      }
    
    },
    login_fukuan:function () {
      let that = this;
      var token=wx.getStorageSync('token')

      if(token!=''){
        that.getname();
            wx.navigateTo({
              url: '../wait_Pay/wait_Pay',
              success: function(res) {
              },
            })
      }
      else{
         // 提示登录
         that.islogin();
      }
    },
    login_daifahuo:function () {
      let that = this;
      var token=wx.getStorageSync('token')

      if(token!=''){
        that.getname();
            wx.navigateTo({
              url: '../wait_Send/wait_Send',
              success: function(res) {
              },
            })
      }
      else{
         // 提示登录
         that.islogin();
      }
    },
    login_daishouhuo:function () {
      let that = this;
      var token=wx.getStorageSync('token')

      if(token!=''){
        that.getname();
            wx.navigateTo({
              url: '../wait_Goods/wait_Goods',
              success: function(res) {
              },
            })
      }
      else{
         // 提示登录
         that.islogin();
      }
    },
    login_shouhou:function () {
      let that = this;
      var token=wx.getStorageSync('token')

      if(token!=''){
        that.getname();
            wx.navigateTo({
              url: '../after_Sale/after_Sale',
              success: function(res) {
              },
            })
      }
      else{
         // 提示登录
         that.islogin();
      }
    },
    login_shopcart:function () {
      let that = this;
      var token=wx.getStorageSync('token')

      if(token!=''){
        that.getname();
            wx.navigateTo({
              url: '../shopping_Cart/shopping_Cart',
              success: function(res) {
              },
            })
      }
      else{
         // 提示登录
         that.islogin();
      }
    
    },
    login_shopaddress:function () {
      let that = this;
      var token=wx.getStorageSync('token')

      if(token!=''){
        that.getname();
            wx.navigateTo({
              url: '../shopping_Address/shopping_Address',
              success: function(res) {
              },
            })
      }
      else{
         // 提示登录
         that.islogin();
      }
    },
    login_personInfo:function () {
      let that = this;
      var token=wx.getStorageSync('token')

      if(token!=''){
        that.getname();
            wx.navigateTo({
              url: '../personal_Info/personal_Info',
              success: function(res) {
              },
            })
      }
      else{
         // 提示登录
         that.islogin();
      }
    },
})
